package com.gome.oa.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.gome.oa.dialog.PopupBrowserDialog;
import com.gome.oa.ess.ExportExcel;
import com.gome.oa.util.SwingUtil;

/**
 *
 * @author: tang li
 * @date: 2016年8月22日
 * @desc: 
 *
 */
public class EssLoadActionListener implements ActionListener, Runnable {
	
	private PopupBrowserDialog popupBrowserDialog;
	private JTextField textEssUsername;
	private JPasswordField textEssPassword;
	private JTextField textStartDate;
	private JTextField textEndDate;
	private JTextField textFilePath;
	private JButton btnLoad;
	
	public EssLoadActionListener(PopupBrowserDialog popupBrowserDialog, JTextField textEssUsername,
			JPasswordField textEssPassword, JTextField textStartDate, JTextField textEndDate, JTextField textFilePath, JButton btnLoad) {
		super();
		this.popupBrowserDialog = popupBrowserDialog;
		this.textEssUsername = textEssUsername;
		this.textEssPassword = textEssPassword;
		this.textStartDate = textStartDate;
		this.textEndDate = textEndDate;
		this.textFilePath = textFilePath;
		this.btnLoad = btnLoad;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		
		if(textEssUsername.getText() == null || textEssUsername.getText().trim().equals("")) {
			SwingUtil.alert(popupBrowserDialog, "请输入用户名", textEssUsername);
			return;
		}
		
		if(textEssPassword.getText() == null || textEssPassword.getText().trim().equals("")) {
			SwingUtil.alert(popupBrowserDialog, "请输入密码", textEssPassword);
			return;
		}
		
//		if(!DateUtil.isDate(textStartDate.getText())) {
//			SwingUtil.alert(popupBrowserDialog, "请正确输入日期", textStartDate);
//			return;
//		}
		
//		if(!DateUtil.isDate(textEndDate.getText())) {
//			SwingUtil.alert(popupBrowserDialog, "请正确输入日期", textEndDate);
//			return;
//		}
		
		Thread thread = new Thread(this);
		thread.start();
		
	}

	@Override
	public void run() {
		SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
            	btnLoad.setEnabled(false);
            	textFilePath.setText("");
            }
        });
		ExportExcel exportExcel = new ExportExcel();
		exportExcel.setUsername(textEssUsername.getText());
		exportExcel.setPassword(new String(textEssPassword.getPassword()));
		try {
			exportExcel.setStartDate(textStartDate.getText());
			exportExcel.setEndDate(textEndDate.getText());
			final String savePath = exportExcel.export(0);
			if(savePath != null) {
				SwingUtilities.invokeLater(new Runnable() {
	                @Override
	                public void run() {
	                	textFilePath.setText(savePath);
	                }
	            });
			}
		} catch (final Exception e) {
			e.printStackTrace();
			SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                	JOptionPane.showConfirmDialog(popupBrowserDialog, e.getMessage(), "信息提示", JOptionPane.DEFAULT_OPTION);
                }
            });
		}
		finally {
			SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                	btnLoad.setEnabled(true);
                }
            });
		}
	}

}
